<template>

  <el-dialog
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    append-to-body
    title="审核申请"
    width="500px"
    @close="handleClose"
  >

    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="left" label-width="100px">

      <el-form-item label="审核意见" prop="auditState">
        <el-radio-group v-model="postForm.auditState">
          <el-radio :label="1">采纳</el-radio>
          <el-radio :label="2">拒绝</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="审核理由" prop="auditMsg">
        <el-input v-model="postForm.auditMsg" size="small" type="textarea"/>
      </el-form-item>

      <el-form-item label="积分奖励" prop="auditPoints">
        <el-input-number v-model="postForm.auditPoints" :precision="0" :min="0" :max="999999" size="small"/>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="handAudit">确 定</el-button>
    </div>

  </el-dialog>

</template>

<script>

import { audit } from '@/api/qu/report'
export default {
  name: 'ReportAuditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    ids: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      postForm: {
        ids: [],
        auditState: 1,
        auditPoints: 0,
        auditMsg: ''
      },
      rules: {
        auditState: [
          { required: true, message: '审核意见不能为空！' }
        ]
      }
    }
  },

  watch: {

    visible: {
      handler() {
        this.dialogVisible = this.visible
      }
    },
    ids: {
      handler(val) {
        this.postForm.ids = val
      }
    },

    'postForm.auditState': {
      handler(val) {
        const rules = {
          auditState: [{ required: true, message: '审核意见不能为空！' }]
        }

        if (val === 2) {
          rules['auditMsg'] = [{ required: true, message: '审核理由不能为空！' }]
        }
        this.rules = rules

        this.$nextTick(() => {
          this.$refs.postForm.clearValidate()
        })
      }
    }
  },

  created() {
    this.dialogVisible = this.visible
  },

  methods: {

    // 提交审核
    handAudit() {
      this.$refs.postForm.validate((valid) => {
        if (!valid) {
          return
        }
        this.loading = true
        audit(this.postForm).then(() => {
          this.$message.success('操作成功！')
          this.loading = false
          this.handleClose()
        })
      })
    },

    handleClose() {
      // 清空
      this.postForm.auditState = null
      this.postForm.auditPoints = 0
      this.postForm.auditMsg = ''
      this.$emit('update:visible', false)
      this.$emit('callback')
    }
  }
}
</script>
